<?php
//字符串预习
?>

<?php 
//声明变量以供测试使用
    $total = 12.628;
    $total_shipping = 3.1415926;
    $subject = "Feedback from web site";
    $email = "admin@bigcustomer.com";
    $feedback = "我常常感慨，ls过去的时光总是在追逐着我，ls让我难以平复。";

    echo "<h2>printf()函数使用\r\n</h2>";
    echo "变量/$/total存储的数字是：$total.\n<br />";
    printf ("变量/$/total存储的数字是：%s.\n<br />", $total);
    printf ("现在小数位是两个:%.2f.\n<br />", $total);
    printf ("字符串参数1： %.2f，字符串参数2：%s.\n<br />", $total, $total_shipping);
    printf ("<h3>带序号的参数方式</h3>");
    printf ("字符串参数1： %2\$.2f，字符串参数2：%1\$s.\n<br />", $total, $total_shipping);//更改顺序
    printf ("<h3>字符串大小写转换</h3>\n");
    echo strtoupper($subject)."<br />";
    echo strtolower($subject)."<br />";
    echo ucfirst($subject)."<br />";
    echo ucwords($subject)."<br />\n";


    echo "<h2>使用字符串函数连接和分割字符串</h2>";
    echo "<h3>使用函数explode()</h3>\n";
    $email_array = explode('@', $email);
    if (strtolower( $email_array[1] )  == "bigcustomer.com") {
        $toaddress = "bob@example.com";
        echo "你是大客户";
    } else {
        $toaddress = "feedback@example.com";
        echo "你不是大客户";
    };

    echo "<h3>使用函数join()</h3>\n";
    $new_email = join('#', $email_array);
    echo $new_email;


    echo "<h3>使用函数strtok()</h3>\n";
    echo "变量原文：".$feedback."<br />";
    $token = strtok($feedback, "ls");
    while ($token != "") {
        echo $token."<br />";
        $token = strtok("ls");//在首次调用后，该函数仅需要 split 参数，这是因为它清楚自己在当前字符串中所在的位置。
    }

    $tokens = strtok($feedback, "ls");
    echo "111".$tokens."222";
    $tokens = strtok( "ls");//重置指针
    echo "111".$tokens."222";
    $tokens = strtok( "ls");
    echo "111".$tokens."222";


    echo "<h3>使用函数substr()</h3>\n";
    $test = 'Your customer service is excellent';
    echo 'substr($test, 6)<br />';
    echo $test.'<br />';
    echo substr($test, 6).'<br />';
    echo 'substr($test, -9)<br />';
    echo $test.'<br />';
    echo substr($test, -9).'<br />';
    echo 'substr($test, 0, 4)<br />';
    echo $test.'<br />';
    echo substr($test, 0, 4).'<br />';


    echo "<h2>4-4：字符串比较</h2>\n";
    echo "<h3>使用函数字符串排序</h3>\n";
    $a = 2;
    $b = 2;

    if (strcmp($a, $b)) {
        echo '执行';
    } else {
        echo '不执行';
    };


    echo "<h3>使用函数strlen()判断长度</h3>\n";
    echo strlen('111').'<br />';
    if (strlen($email) < 6) {
        echo '您的邮件地址不正确，请重新填写';
        exit;//force termination of execution - 强制终止执行
    } else {
        echo '您填写的邮件地址正确';
    };

    
    echo "<h2>4-5：使用字符串函数匹配和替换子字符串</h2>\n";

    $n_test = '1的两句，我简单说两句，让我不再单1111111111';
    $n_tests = '我简单说两句，这次的事情还是蛮简单的，大家简单点来就行。';
    echo strstr($n_test, '简单').'<br />';

    $text = 'Nice to me too';
    $result = strpos($text, "to");//php是弱类型语言
    echo $result;
    if ($result === false) {//=赋值，==值相同，===值和类型都相同
        echo '你没有to';
    } else {
        echo '你在to';
    };

    $test_a = '哦，你们的产品真是太糟糕了,简直是太坏了';
    $offcolor = array ('太糟糕', '太坏了',);

    $test_a = str_replace($offcolor, '%!@*', $test_a, $sg);
    echo '<br />'.$test_a;
    echo '<br />您的脏话已替换次数：'.$sg;

    echo '<h2>substr_replace()函数</h2>';
    $test_b = 'this is a x no ideal';
    echo $test_b.'<br />';
    $test_b = substr_replace($test_b, 'X-S', -6);
    echo $test_b;


    echo '<h2>4-6：正则</h2>\n';
    echo '<h3>4-7：用正则表达式查找字符串 - preg_match()</h3>\n';

    $email = 'admi(n@npc.com';
    $feedback = 'shospa@npc.com';
    if (preg_match('/^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/', $email) === 0) {
        echo '收件地址不正确，请重新输入';
        //exit;
    }

    $toaddress = 'feedback@example.com';
    if (preg_match('/shop|customer service|retail/', $feedback)) {
        $toaddress = 'retail@example.com';
    }

    if (preg_match('/deliver|fulfill/', $feedback)) {
        $toaddress = 'fulfilment@example.com';
    }

    if (preg_match('/bill|account/', $feedback)) {
        $toaddress = 'accounts@example.com';
    }

    if (preg_match('/bigcustomer\.com/', $email)) {
        $toaddress = 'bob@example.com';
    }

    echo $toaddress;


    echo '<h3>4-8：用正则表达式替换子字符串 - preg_replace</h3>';

    $str = 'runo o   b';
    $str = preg_replace('/\s+/', '空白', $str);
    // 将会改变为'runoob'
    echo $str;


    echo '<h3>4-9：用正则表达式分割字符串 - preg_replace</h3>';
    $address = 'username@example.com';
    $arr = preg_split ('/\.|@/', $address);
    //each()返回当前元素的键名($key)和键值($value)，并将内部指针向后移动
    //list() 函数用于在一次操作中给一组变量赋值。
    while (list($key, $value) = each ($arr)) {
        echo '<br />'.$key.'：'.$value;
    };
















?>
